package com.dream.common.utils.payutils;


import com.dream.common.utils.payutils.bean.AliRedPackageBean;
import com.dream.common.utils.payutils.bean.BeforeRedPackageBean;
import com.dream.common.utils.payutils.bean.WorkedRedPackageBean;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class WishingUtils {
    public static WorkedRedPackageBean setWishing(BeforeRedPackageBean beforeRedPackageBean, WorkedRedPackageBean workedRedPackageBean) {
        String beforeWishing = beforeRedPackageBean.blessings.replace("恭喜发财，大吉大利", "1");
        String afterWishing;
//        double discount = configurationBean.discount;
//        double discount = 10;
        int sendAmount = (int) Double.parseDouble(beforeRedPackageBean.resultData.totalfee);
        afterWishing = beforeWishing.replace(sendAmount + "", "");
        afterWishing = afterWishing.replace("壹", "1");
        afterWishing = afterWishing.replace("贰", "2");
        afterWishing = afterWishing.replace("叁", "3");
        afterWishing = afterWishing.replace("肆", "4");
        afterWishing = afterWishing.replace("伍", "5");
        afterWishing = afterWishing.replace("陆", "6");
        afterWishing = afterWishing.replace("柒", "7");
        afterWishing = afterWishing.replace("捌", "8");
        afterWishing = afterWishing.replace("玖", "9");
        afterWishing = afterWishing.replace("零", "0");

        if (!NumberUtils.isDigit(afterWishing)) {
            String thunder = "";
            char[] strChar = afterWishing.toCharArray();
            for (int i = strChar.length; i > 0; i--) {
                if (Character.isDigit(strChar[i - 1])) {
                    thunder = thunder + strChar[i - 1];
                } else {
                    break;
                }
            }
            afterWishing = thunder;
        }
        if ("".equals(afterWishing)) {
            workedRedPackageBean.wishing = new Double(workedRedPackageBean.sendMoney).intValue() + "";
        } else {
            workedRedPackageBean.wishing = afterWishing;
        }
        return workedRedPackageBean;
    }

    public static void main(String[] args) {
        AliRedPackageBean aliRedPackageBean = new AliRedPackageBean();
        aliRedPackageBean.blessings = "不2333";
        aliRedPackageBean.totalFee = "40.00";
        String discountInfo = "30=40";
        String workedBlessings = getWorkedBlessings(aliRedPackageBean, discountInfo);
        System.out.println(workedBlessings);
    }

    public static String getWorkedBlessings(AliRedPackageBean aliRedPackageBean, String discountInfo) {
        Map<Integer, Integer> discountMap = getDiscountMap(discountInfo);
        String beforeBlessings = aliRedPackageBean.blessings;
        if (beforeBlessings.contains("恭喜发财，万事如意！")) {
            beforeBlessings = "1";
        }
        int sendAmount = (int) Double.parseDouble(aliRedPackageBean.totalFee);
        if (discountMap != null && discountMap.containsKey(sendAmount)) {
            sendAmount = discountMap.get(sendAmount);
        }
        return getWishing(beforeBlessings, sendAmount);
    }

    private static String getWishing(String beforeBlessings, int sendAmount) {
        String workedBlessings = "";
        beforeBlessings = beforeBlessings.trim();
        beforeBlessings = beforeBlessings.replace("+", "/");
        beforeBlessings = beforeBlessings.replace("-", "/");
        beforeBlessings = beforeBlessings.replace("*", "/");
        beforeBlessings = beforeBlessings.replace("@", "/");
        beforeBlessings = beforeBlessings.replace("$", "/");
        beforeBlessings = beforeBlessings.replace("%", "/");
        beforeBlessings = beforeBlessings.replace("¥", "/");
        beforeBlessings = beforeBlessings.replace("#", "/");
        beforeBlessings = beforeBlessings.replace("，", "/");
        beforeBlessings = beforeBlessings.replace("。", "/");
        beforeBlessings = beforeBlessings.replace("！", "/");
        beforeBlessings = beforeBlessings.replace("？", "/");
        beforeBlessings = beforeBlessings.replace(",", "/");
        beforeBlessings = beforeBlessings.replace(".", "/");
        beforeBlessings = beforeBlessings.replace("!", "/");
        beforeBlessings = beforeBlessings.replace("?", "/");
        beforeBlessings = beforeBlessings.replace(":", "/");
        beforeBlessings = beforeBlessings.replace("：", "/");
        beforeBlessings = beforeBlessings.replace("、", "/");
        while (beforeBlessings.contains("//")) {
            beforeBlessings = beforeBlessings.replace("//", "/");
        }
        if (beforeBlessings.contains("/")) {
            String thunder = "";
            char[] strChar = beforeBlessings.toCharArray();
            for (int i = 0; i < strChar.length; i++) {
                if (Character.isDigit(strChar[i]) || String.valueOf(strChar[i]).equals("/")) {
                    thunder = thunder + strChar[i];
                } else {
                    break;
                }
            }
            String[] thunderArr = thunder.split("/");
            if (thunderArr != null && thunderArr.length == 2) {
                workedBlessings = thunderArr[1];
            }
        } else {
            String thunder = "";
            char[] strChar = beforeBlessings.toCharArray();
            for (int i = 0; i < strChar.length; i++) {
                if (Character.isDigit(strChar[i])) {
                    thunder = thunder + strChar[i];
                } else {
                    continue;
                }
            }
            if (thunder.contains(sendAmount + "")) {
                int thunderAmount = thunder.replace(sendAmount + "", "").length();
                if (thunderAmount + (sendAmount + "").length() != thunder.length()) {
                    workedBlessings = sendAmount + thunder.replace(sendAmount + "", "");
                } else {
                    workedBlessings = thunder.replace(sendAmount + "", "");
                }
            } else {
                workedBlessings = thunder;
            }
        }
        workedBlessings = removeRepeatChar(workedBlessings);
        if (StringUtils.isEmpty(workedBlessings)) {
            return sendAmount + "";
        }
        return workedBlessings;
    }

    public static Map<Integer, Integer> getDiscountMap(String discount) {
        Map<Integer, Integer> discountMap = new HashMap();
        if (discount == null || "".equals(discount)) {
            return discountMap;
        }
        char[] discountChar = discount.toCharArray();
        String workedDiscountStr = "";
        for (int i = 0; i < discountChar.length; i++) {
            if (Character.isDigit(discountChar[i]) || String.valueOf(discountChar[i]).equals("/") || (discountChar[i] + "").equals("=")) {
                workedDiscountStr = workedDiscountStr + discountChar[i];
            }
        }
        if (!"".equals(workedDiscountStr)) {
            String[] discountArr = workedDiscountStr.split("/");
            for (String discountStr : discountArr) {
                String[] discountMoneyArr = discountStr.split("=");
                if (discountMoneyArr != null && discountMoneyArr.length == 2) {
                    discountMap.put(Integer.parseInt(discountMoneyArr[0]), Integer.parseInt(discountMoneyArr[1]));
                }
            }
        }
        return discountMap;
    }

    public static String removeRepeatChar(String str) {
        if (str == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        char[] strChar = str.toCharArray();
        for (int i = 0; i < strChar.length; i++) {
            if (!sb.toString().contains(String.valueOf(strChar[i]))) {
                sb.append(strChar[i]);
            }
        }
        return sb.toString();
    }
}
